<?php
	class Hello extends CI_Controller
	{
		function __construct()
		{
			parent::__construct();
			$this->load->model('card_model'); 
		} //end __construct

		function index()
		{
			//startGame()
			$this->start_game();
			//$this->game();
		}
		
		function hand_count($hand)
		{
			return count($hand);
		}

		function set_trump($deck)
		{
			$trump = array_pop($deck);
			array_unshift($deck, $trump);
			$this->session->set_userdata('deck', $deck);
			return $trump;
		}

		function set_player1_hand($hand, $deck)
		{
			for($i = $this->hand_count($hand); $i < 6; $i++)
			{
				$card = array_pop($deck);
				array_push($hand, $card);
			}
			$this->session->set_userdata('hand1', $hand);
			$this->session->set_userdata('deck', $deck);
		}

		function set_player2_hand($hand, $deck)
		{
			for($i = $this->hand_count($hand); $i < 6; $i++)
			{
				$card = array_pop($deck);
				array_push($hand, $card);
			}
			$this->session->set_userdata('hand2', $hand);
			$this->session->set_userdata('deck', $deck);
		}

		function get_deck()
		{
			$suits = $this->card_model->get_suits();
			$faces = $this->card_model->get_faces();
			
			$deck = array();

			foreach ($suits as $suit) {
    			foreach ($faces as $face) {
        			$deck[] = array ("face"=>$face, "suit"=>$suit);
    			}
			} //end foreach
			shuffle($deck);
			$deck = $this->session->set_userdata('deck', $deck);
			return $this->session->userdata('deck');
		} //end get_deck()

		/*function set_session()
		{
			$session_data = array(
				'deck' => [],
				'hand1' => [],
				'hand2' => [],
				'table' => [],
				'trump' => [],
				'p1_score' => 0,
				'p2_score' => 0
				);
			$this->session->set_userdata($session_data);
		}*/

		public function start_game()
		{
			//On Click of Play
			//$this->set_session();
			$deck = $this->get_deck();
			$hand1 = $this->session->userdata('hand1');
			$hand2 = $this->session->userdata('hand2');

			if($this->hand_count($hand1) < 6)
			{
				$this->set_player1_hand($hand1, $deck);
				$hand1 = $this->session->userdata('hand1');
				$this->session->set_userdata('hand1', $hand1);
				$deck = $this->session->userdata('deck');	
			}

			if($this->hand_count($hand2) < 6)
			{
				$this->set_player2_hand($hand2, $deck);
				$hand2 = $this->session->userdata('hand2');
				$deck = $this->session->userdata('deck');
			}
			$trump = $this->session->userdata('trump');
			$trump = $this->set_trump($deck);
			$deck = $this->session->userdata('deck');
			$this->session->set_userdata('trump', $trump);
			$data['table'] = $this->session->userdata('table');
			$player['first'] = $hand1;
			$player['second'] = $hand2;
			$player['trump'] = $trump;
			//initializing finishes here
			print_r($this->session->all_userdata());

			
			$this->load->view('durak/player1', $player);
			$this->load->view('durak/table', $data);
			$this->load->view('durak/player2', $player);
		}

		function game()
		{
			$deck = $this->session->userdata('deck');
			$table = $this->session->userdata('table');
			$this->get_first();
			$hand1 = $this->session->userdata('hand1');
			$hand2 = $this->session->userdata('hand2');
		}

		public function p() {

			print_r($this->session->all_userdata());
			
		}
		public function p1_attack($index)
		{

		
		} //end p1_attack

		
	} //end of class Durak